(function () {
  let runOnce = function (cb) {
    let num = 0

    return function () {
      if (num < 1) {
        num = num++
        cb && cb()
      }
    }
  }

  let k = 'test-main'
  let runId = 'a7'
  let vvg = window.vvg
  vvg.elements[runId] = {
    attrs: {
      'data-com-type': k,
      'data-can-child': "true",
      'data-run-id': runId
    },
    textContent: k,
    run : runOnce(function () {
      window.getVma('pageapp').$refs.com.initExt(k, (vvg.page.ext[k].toString()))
    })
  }

  vvg.page.ext[k] = `
<template id="${k}-tpl">
  <div class="test-main">
    <div class="test-main-inner">
      <template v-for="item in items">
           <img :src="item" alt="" />
      </template>
    </div>
    <slot></slot>
  </div>
</template>
<template id="${k}-style-tpl">
.test-main {
  display: block;
}
</template>
<script>
(function() {
  console.log(window.addStyleSheetFromTemplate)
if (window.addStyleSheetFromTemplate) {
   window.addStyleSheetFromTemplate('${k}')
}

Vue.component('${k}', {
 template: '#'+'${k}'+'-tpl',
  props: {
    sstyle: {
      type: String,
      default: JSON.stringify({})
    },
    itemsstr: String
  },
  data() {
   return {
     items: []
   }
  },
  watch: {
    sstyle(newVal) {
      this.genStyle(newVal)
    },
    itemsstr(newVal) {
      this.genItems(newVal)
    }
  },
  mounted() {
   this.genStyle(this.sstyle)
   this.genItems(this.itemsstr)
  },
  methods: {
    genStyle(sstyle) {
      if (window.cssobj) {
         window.cssobj(JSON5.parse(sstyle))
      }
    },
    genItems(v) {
       if (v) {
        this.items = v.split(',')
      } else {
        this.items = []
      }
    }
  }
})
})()
</script>
    `.trim()

  vvg.page.des[k] = function() {
    return {
      '.test-main': {
        type: 'style',
        value: '',
        for: 'sstyle',
      },
      '.test-main-inner': {
        type: 'style',
        value: '',
        for: 'sstyle',
      },
      '.test-main img': {
        type: 'style',
        value: '',
        for: 'sstyle',
      },
    }
  }

  console.log('run ', k)
})()
